package com.usian.article.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.article.mapper.ApArticleConfigMapper;
import com.usian.article.mapper.ApArticleContentMapper;
import com.usian.article.mapper.ApArticleMapper;
import com.usian.article.service.ApArticleService;
import com.usian.common.constants.ArticleConstans;
import com.usian.model.article.dtos.ArticleHomeDto;
import com.usian.model.article.dtos.ArticleInfoDto;
import com.usian.model.article.pojos.ApArticle;
import com.usian.model.article.pojos.ApArticleConfig;
import com.usian.model.article.pojos.ApArticleContent;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class ApArticleServiceImpl extends BaseServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {
    @Autowired
    private ApArticleConfigMapper configMapper;

    @Autowired
    private ApArticleContentMapper contentMapper;

    @Override
    public ResponseResult load(Short loadtype, ArticleHomeDto dto) {
        if (loadtype == null) loadtype = ArticleConstans.LOADTYPE_LOAD_MORE;
        dto.setSize(Math.min(50, dto.getSize()));
        if (StringUtils.isBlank(dto.getTag())) dto.setTag(ArticleConstans.DEFAULT_TAG);
        List<ApArticle> apArticles = baseMapper.loadArticleList(dto, loadtype);
        ResponseResult<List<ApArticle>> result = new ResponseResult<>();
        result.setData(apArticles);
        return result;
    }

    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {
        if (dto == null || dto.getArticleId() == null) return paramError;
        ApArticleConfig config = configMapper.selectOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, dto.getArticleId()));
        if (config==null)return noData;
        if (config.getIsDelete()||config.getIsDown()){
            return ResponseResult.okResult(config.getIsDelete()?"文章已删除":"文章已下架");
        }
        ApArticleContent content = contentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getArticleId()));
        HashMap<Object, Object> map = new HashMap<>();
        map.put("config",config);
        map.put("content",content);
        return ResponseResult.okResult(map);
    }
}
